package com.lineage.server.model;

import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.model.Instance.L1SummonInstance;
import com.lineage.server.model.Instance.L1TowerInstance;
import com.lineage.server.serverpackets.S_Paralysis;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import java.util.logging.Logger;

public class L1Poison7 {
  private static final Logger _log = Logger.getLogger(L1Poison7.class.getName());
  
  private L1Object _l1object = null;
  
  private L1PcInstance _player = null;
  
  private L1MonsterInstance _mob = null;
  
  private L1PetInstance _pet = null;
  
  private L1SummonInstance _summon = null;
  
  private L1TowerInstance _tower = null;
  
  private L1NpcInstance _npc = null;
  
  private int _targetId = 0;
  
  private boolean _isPoisonTimerDelete = false;
  
  private boolean _isParalysisPoisonTimerDelete = false;
  
  private int _time;
  
  private int _damage;
  
  private NormalPoisonTimer _normal_poison_timer;
  
  private ParalysisPoisonTimer _paralysis_poison_timer;
  
  private ParalysisTimer _paralysis_timer;
  
  private static L1Poison7 _instance;
  
  public static L1Poison7 getInstance() {
    if (_instance == null)
      _instance = new L1Poison7(); 
    return _instance;
  }
  
  public boolean handleCommands(L1Object l1object, int type, int time, int damage) {
    if (l1object == null)
      return false; 
    if (l1object instanceof L1PcInstance) {
      this._player = (L1PcInstance)l1object;
      this._targetId = this._player.getId();
      if (this._player.get_poisonStatus7() != 0)
        return false; 
      this._player.set_poisonStatus7(type);
    } else if (l1object instanceof L1MonsterInstance) {
      this._mob = (L1MonsterInstance)l1object;
      this._targetId = this._mob.getId();
      if (this._mob.get_poisonStatus7() != 0)
        return false; 
      this._mob.set_poisonStatus7(type);
    } else if (l1object instanceof L1PetInstance) {
      this._pet = (L1PetInstance)l1object;
      this._targetId = this._pet.getId();
      if (this._pet.get_poisonStatus7() != 0)
        return false; 
      this._pet.set_poisonStatus7(type);
    } else if (l1object instanceof L1SummonInstance) {
      this._summon = (L1SummonInstance)l1object;
      this._targetId = this._summon.getId();
      if (this._summon.get_poisonStatus7() != 0)
        return false; 
      this._summon.set_poisonStatus7(type);
    } else {
      if (l1object instanceof L1TowerInstance) {
        type = 1;
        this._tower = (L1TowerInstance)l1object;
        this._targetId = this._tower.getId();
        this._tower.set_poisonStatus7(type);
        return true;
      } 
      type = 1;
      this._npc = (L1NpcInstance)l1object;
      this._targetId = this._npc.getId();
      this._npc.set_poisonStatus7(type);
      return true;
    } 
    if (type == 1) {
      this._normal_poison_timer = new NormalPoisonTimer(l1object, time, damage);
      GeneralThreadPool.get().execute(this._normal_poison_timer);
    } else if (type == 4) {
      this._paralysis_poison_timer = new ParalysisPoisonTimer(l1object, time);
      GeneralThreadPool.get().execute(this._paralysis_poison_timer);
    } 
    return true;
  }
  
  public void CurePoison(L1Object l1object) {
    if (l1object instanceof L1PcInstance) {
      this._player = (L1PcInstance)l1object;
      this._targetId = this._player.getId();
      if (this._player.get_poisonStatus7() == 1) {
        this._isPoisonTimerDelete = true;
      } else {
        if (this._player.get_poisonStatus7() != 4)
          return; 
        this._isParalysisPoisonTimerDelete = true;
      } 
      this._player.set_poisonStatus7(0);
    } else if (l1object instanceof L1MonsterInstance) {
      this._mob = (L1MonsterInstance)l1object;
      this._targetId = this._mob.getId();
      if (this._mob.get_poisonStatus7() == 1) {
        this._isPoisonTimerDelete = true;
      } else {
        if (this._mob.get_poisonStatus7() != 4)
          return; 
        this._isParalysisPoisonTimerDelete = true;
      } 
      this._mob.set_poisonStatus7(0);
    } else if (l1object instanceof L1PetInstance) {
      this._pet = (L1PetInstance)l1object;
      this._targetId = this._pet.getId();
      if (this._pet.get_poisonStatus7() == 1) {
        this._isPoisonTimerDelete = true;
      } else {
        if (this._pet.get_poisonStatus7() != 4)
          return; 
        this._isParalysisPoisonTimerDelete = true;
      } 
      this._pet.set_poisonStatus7(0);
    } else if (l1object instanceof L1SummonInstance) {
      this._summon = (L1SummonInstance)l1object;
      this._targetId = this._summon.getId();
      if (this._summon.get_poisonStatus7() == 1) {
        this._isPoisonTimerDelete = true;
      } else {
        if (this._summon.get_poisonStatus7() != 4)
          return; 
        this._isParalysisPoisonTimerDelete = true;
      } 
      this._summon.set_poisonStatus7(0);
    } else if (l1object instanceof L1TowerInstance) {
      this._tower = (L1TowerInstance)l1object;
      this._targetId = this._tower.getId();
      this._tower.set_poisonStatus7(0);
    } else {
      this._npc = (L1NpcInstance)l1object;
      this._targetId = this._npc.getId();
      this._npc.set_poisonStatus7(0);
    } 
  }
  
  class NormalPoisonTimer extends Thread {
    public NormalPoisonTimer(L1Object l1object, int time, int damage) {
      L1Poison7.this._l1object = l1object;
      L1Poison7.this._time = time;
      L1Poison7.this._damage = damage;
    }
    
    public void run() {
      while (true) {
        try {
          Thread.sleep(L1Poison7.this._time);
        } catch (Exception exception) {
          break;
        } 
        if (L1Poison7.this._isPoisonTimerDelete)
          return; 
        if (L1Poison7.this._l1object instanceof L1PcInstance) {
          L1Poison7.this._player = (L1PcInstance)L1Poison7.this._l1object;
          L1Poison7.this._player.receiveDamage((L1Character)L1Poison7.this._l1object, 
              L1Poison7.this._damage, L1Poison7.this._damage);
          if (L1Poison7.this._player.isDead())
            break; 
          continue;
        } 
        if (L1Poison7.this._l1object instanceof L1MonsterInstance) {
          L1Poison7.this._mob = (L1MonsterInstance)L1Poison7.this._l1object;
          L1Poison7.this._mob.receiveDamage((L1Character)L1Poison7.this._l1object, L1Poison7.this._damage);
          if (L1Poison7.this._mob.isDead())
            return; 
          continue;
        } 
        if (L1Poison7.this._l1object instanceof L1PetInstance) {
          L1Poison7.this._pet = (L1PetInstance)L1Poison7.this._l1object;
          L1Poison7.this._pet.receiveDamage((L1Character)L1Poison7.this._l1object, L1Poison7.this._damage);
          if (L1Poison7.this._pet.isDead())
            return; 
          continue;
        } 
        if (!(L1Poison7.this._l1object instanceof L1SummonInstance))
          continue; 
        L1Poison7.this._summon = (L1SummonInstance)L1Poison7.this._l1object;
        L1Poison7.this._summon.receiveDamage((L1Character)L1Poison7.this._l1object, 
            L1Poison7.this._damage);
        if (L1Poison7.this._summon.isDead())
          return; 
      } 
      L1Poison7.this.CurePoison(L1Poison7.this._l1object);
    }
  }
  
  class ParalysisPoisonTimer extends Thread {
    public ParalysisPoisonTimer(L1Object l1object, int time) {
      L1Poison7.this._l1object = l1object;
      L1Poison7.this._time = time;
    }
    
    public void run() {
      try {
        Thread.sleep(10L);
      } catch (Exception exception) {}
      if (L1Poison7.this._isParalysisPoisonTimerDelete)
        return; 
      if (L1Poison7.this._l1object instanceof L1PcInstance) {
        L1Poison7.this._player = (L1PcInstance)L1Poison7.this._l1object;
        if (!L1Poison7.this._player.isDead()) {
          L1Poison7.this._targetId = L1Poison7.this._player.getId();
          L1Poison7.this._player.sendPackets((ServerBasePacket)new S_Paralysis(4, true, 0));
          L1Poison7.this._paralysis_timer = new L1Poison7.ParalysisTimer(L1Poison7.this._l1object, 
              L1Poison7.this._time);
          GeneralThreadPool.get().execute(L1Poison7.this._paralysis_timer);
        } 
      } 
      if (L1Poison7.this._l1object instanceof L1MonsterInstance) {
        L1Poison7.this._mob = (L1MonsterInstance)L1Poison7.this._l1object;
        if (!L1Poison7.this._mob.isDead()) {
          L1Poison7.this._targetId = L1Poison7.this._mob.getId();
          L1Poison7.this._paralysis_timer = new L1Poison7.ParalysisTimer(L1Poison7.this._l1object, 
              L1Poison7.this._time);
          GeneralThreadPool.get().execute(L1Poison7.this._paralysis_timer);
        } 
      } 
      if (L1Poison7.this._l1object instanceof L1PetInstance) {
        L1Poison7.this._pet = (L1PetInstance)L1Poison7.this._l1object;
        if (!L1Poison7.this._pet.isDead()) {
          L1Poison7.this._targetId = L1Poison7.this._pet.getId();
          L1Poison7.this._pet.setParalyzed(true);
          L1Poison7.this._paralysis_timer = new L1Poison7.ParalysisTimer(L1Poison7.this._l1object, 
              L1Poison7.this._time);
          GeneralThreadPool.get().execute(L1Poison7.this._paralysis_timer);
        } 
      } 
      if (L1Poison7.this._l1object instanceof L1SummonInstance) {
        L1Poison7.this._summon = (L1SummonInstance)L1Poison7.this._l1object;
        if (!L1Poison7.this._summon.isDead()) {
          L1Poison7.this._targetId = L1Poison7.this._summon.getId();
          L1Poison7.this._summon.setParalyzed(true);
          L1Poison7.this._paralysis_timer = new L1Poison7.ParalysisTimer(L1Poison7.this._l1object, 
              L1Poison7.this._time);
          GeneralThreadPool.get().execute(L1Poison7.this._paralysis_timer);
        } 
      } 
    }
  }
  
  class ParalysisTimer extends Thread {
    public ParalysisTimer(L1Object l1object, int time) {
      L1Poison7.this._l1object = l1object;
      L1Poison7.this._time = time;
    }
    
    public void run() {
      try {
        Thread.sleep(L1Poison7.this._time);
      } catch (Exception exception) {}
      if (L1Poison7.this._l1object instanceof L1PcInstance) {
        L1Poison7.this._player = (L1PcInstance)L1Poison7.this._l1object;
        if (!L1Poison7.this._player.isDead()) {
          L1Poison7.this._player.sendPackets((ServerBasePacket)new S_Paralysis(4, false, 0));
          L1Poison7.this.CurePoison(L1Poison7.this._l1object);
        } 
      } 
      if (L1Poison7.this._l1object instanceof L1MonsterInstance) {
        L1Poison7.this._mob = (L1MonsterInstance)L1Poison7.this._l1object;
        if (!L1Poison7.this._mob.isDead())
          L1Poison7.this.CurePoison(L1Poison7.this._l1object); 
      } 
      if (L1Poison7.this._l1object instanceof L1PetInstance) {
        L1Poison7.this._pet = (L1PetInstance)L1Poison7.this._l1object;
        if (!L1Poison7.this._pet.isDead()) {
          L1Poison7.this._pet.setParalyzed(false);
          L1Poison7.this.CurePoison(L1Poison7.this._l1object);
        } 
      } 
      if (L1Poison7.this._l1object instanceof L1SummonInstance) {
        L1Poison7.this._summon = (L1SummonInstance)L1Poison7.this._l1object;
        if (!L1Poison7.this._summon.isDead()) {
          L1Poison7.this._summon.setParalyzed(false);
          L1Poison7.this.CurePoison(L1Poison7.this._l1object);
        } 
      } 
    }
  }
}
